package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 里
 * @Description:
 */
@Service
public class SupplierServiceImpl implements SupplierService {
    //注入dao层
    @Autowired
    private SupplierDao supplierDao;

    @Override
    public Map<String,Object> getSupplierPageList(Integer page, Integer rows, String supplierName) {

        //定义一个map集合 用作返回key-》value数据
        Map<String,Object> supplierMap = new HashMap<>();
        Integer pageNum = (page-1)*rows;
        List<Supplier> supplierList =  supplierDao.getSupplierPageList(pageNum,rows,supplierName);
        //判断集合是否为空，不为空则遍历到map集合中
        if (supplierList != null) {
            supplierMap.put("total",supplierDao.getSupplierCount());
            supplierMap.put("rows",supplierList);
        }
        return supplierMap;
    }


    @Override
    public void saveOrUpdate(Supplier supplier) {
        // 判断对象当中是否有传id，如果有则走修改方法
        if (supplier.getSupplierId() != null) {
            supplier =  supplierDao.getSupplierlist(supplier.getSupplierId());
            supplierDao.updateSupplier(supplier);
        }
        else {//否则添加客户
            supplierDao.save(supplier);
        }
    }

    @Override
    public ServiceVO deleteById(List<String> ids) {
        supplierDao.deleteById(ids);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }
}
